package com.liangsui.mss.controller.system;

import com.liangsui.mss.dto.DictTypeDto;
import com.liangsui.mss.service.DictTypeService;
import com.liangsui.mss.utils.ShiroSecurityUtils;
import com.liangsui.mss.vo.AjaxResult;
import com.liangsui.mss.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * DictTypeController
 *
 * @author zhangjiawei
 * @email 1533167396@qq.com
 * @date 2022/12/4 18:42
 */

@RestController
@RequestMapping("system/dict/type")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 分页查询字典类型列表
     * @param
     * @return
     */
    @GetMapping("listForPage")
    public AjaxResult listForPage(DictTypeDto dictTypeDto) {
        DataGridView dataGridView = dictTypeService.listForPage(dictTypeDto);
        return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
    }

    /**
     * 同步缓存
     * @return
     */
    @GetMapping("dictCacheAsync")
    public AjaxResult dictCacheAsync() {
        try {
            dictTypeService.dictCacheAsync();
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * @功能描述: 字典类型添加
     */
    @PostMapping("addDictType")
    public AjaxResult addDictType(@Validated DictTypeDto dictTypeDto) {
        // 	校验数据是否存在
        if (this.dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(), dictTypeDto.getDictType())) {
            return AjaxResult.fail("新增字典【" + dictTypeDto.getDictName() + "】失败，字典类型已存在!");
        }
        // 设置添加人员
        dictTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictTypeService.insert(dictTypeDto));
    }

    /**
     * @功能描述: 字典类型修改
     */
    @PutMapping("updateDictType")
    public AjaxResult updateDictType(@Validated DictTypeDto dictTypeDto) {
        // 	校验数据是否存在
        if (this.dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(), dictTypeDto.getDictType())) {
            return AjaxResult.fail("修改字典【" + dictTypeDto.getDictName() + "】失败，字典类型已存在!");
        }
        // 设置修改人员
        dictTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictTypeService.update(dictTypeDto));
    }

    /**
     * @功能描述: 根据ID查询一个字典信息
     */
    @GetMapping("getOne/{dictId}")
    public AjaxResult getDictOneById(@NotNull(message = "类型字典ID不能为空")
                                     @PathVariable @Validated Long dictId) {
        return AjaxResult.success(this.dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * @功能描述:删除
     */
    @DeleteMapping("deleteDictTypeByIds/{dictIds}")
    public AjaxResult updateDictType(@PathVariable @Validated
                                     @NotEmpty(message = "要删除的类型字典ID不能为空") Long[] dictIds) {
        return AjaxResult.toAjax(this.dictTypeService.deleteDictTypeByIds(dictIds));
    }

    /**
     * @功能描述:查询所有可用的字典类型
     */
    @GetMapping("selectAllDictType")
    public AjaxResult selectAllDictType() {
        return AjaxResult.success(this.dictTypeService.selectAll().getData());
    }

    /**
     * 功能描述: 清理指定缓存数据
     */
    @PostMapping("dictClearCacheAsync")
    public AjaxResult dictClearCacheAsync() {
        try {
            this.dictTypeService.dictClearCacheAsync();
            return AjaxResult.success("字典缓存清理成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
